package BouncingShapes.bounceboxapp;

import BouncingShapes.bouncebox.*;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 *
 * @author ashongtical
 * @author Anka
 */

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        anka();
    }
    
    public static void anka() throws FileNotFoundException {
        BounceBox box = new BounceBox(700, 500);
        FileInputStream fileIn = new FileInputStream("src/practical4.txt");
        Scanner sc = new Scanner(fileIn);
        int circleCount = 0, squareCount = 0, triangleCount = 0, rectangleCount = 0;
        double circleArea = 0.0, squareArea = 0.0, triangleArea = 0.0, rectangleArea = 0.0;
        double totalArea = 0.0;
        while (sc.hasNextLine()) {
            Scanner line = new Scanner(sc.nextLine());
            String shapeType;
            if (line.hasNext()) {
                shapeType = line.next();
            } else {
                continue;
            }
            if (shapeType.equalsIgnoreCase("Circle")) {
                int x = line.nextInt();
                int y = line.nextInt();
                int r = line.nextInt();
                if (line.hasNextInt()) {
                    int vx = line.nextInt();
                    int vy = line.nextInt();
                    if (line.hasNextInt()) {
                        int red = line.nextInt();
                        int green = line.nextInt();
                        int blue = line.nextInt();
                        Circle c1 = new Circle(x, y, r);
                        c1.setColor(new Color(red, green, blue));
                        c1.setVelocity(vx, vy);
                        box.addShape(c1);
                        circleCount++;
                        circleArea += c1.getMass();
                        totalArea += c1.getMass();
                        //Circle created with velocity and color
                    } else {
                        Circle c1 = new Circle(x, y, r);
                        c1.setVelocity(vx, vy);
                        c1.setColor(Color.BLACK);
                        box.addShape(c1);
                        circleCount++;
                        circleArea += c1.getMass();
                        totalArea += c1.getMass();
                        //Circle created with velocity
                    }
                } else {
                    Circle c1 = new Circle(x, y, r);
                    c1.setColor(Color.BLACK);
                    box.addShape(c1);
                    circleCount++;
                    circleArea += c1.getMass();
                    totalArea += c1.getMass();
                    //Circle created without velocity
                }
            } else if (shapeType.equalsIgnoreCase("Square")) {
                int x = line.nextInt();
                int y = line.nextInt();
                int side = line.nextInt();
                if (line.hasNextInt()) {
                    int vx = line.nextInt();
                    int vy = line.nextInt();
                    if (line.hasNextInt()) {
                        int red = line.nextInt();
                        int green = line.nextInt();
                        int blue = line.nextInt();
                        Square s1 = new Square(x, y, side);
                        s1.setColor(new Color(red, green, blue));
                        s1.setVelocity(vx, vy);
                        box.addShape(s1);
                        squareCount++;
                        squareArea += s1.getMass();
                        totalArea += s1.getMass();
                        //Square created with velocity and color
                    } else {
                        Square s1 = new Square(x, y, side);
                        s1.setVelocity(vx, vy);
                        s1.setColor(Color.BLACK);
                        box.addShape(s1);
                        squareCount++;
                        squareArea += s1.getMass();
                        totalArea += s1.getMass();
                        //Square created with velocity
                    }
                } else {
                    Square s1 = new Square(x, y, side);
                    s1.setColor(Color.BLACK);
                    box.addShape(s1);
                    squareCount++;
                    squareArea += s1.getMass();
                    totalArea += s1.getMass();
                    //Square created without velocity
                }
            } else if (shapeType.equalsIgnoreCase("Triangle")) {
                int x = line.nextInt();
                int y = line.nextInt();
                int base = line.nextInt();
                int height = line.nextInt();
                if (line.hasNextInt()) {
                    int vx = line.nextInt();
                    int vy = line.nextInt();
                    if (line.hasNextInt()) {
                        int red = line.nextInt();
                        int green = line.nextInt();
                        int blue = line.nextInt();
                        Triangle t1 = new Triangle(x, y, base, height);
                        t1.setColor(new Color(red, green, blue));
                        t1.setVelocity(vx, vy);
                        box.addShape(t1);
                        triangleCount++;
                        triangleArea += t1.getMass();
                        totalArea += t1.getMass();
                        //Triangle created with velocity and color
                    } else {
                        Triangle t1 = new Triangle(x, y, base, height);
                        t1.setVelocity(vx, vy);
                        t1.setColor(Color.BLACK);
                        box.addShape(t1);
                        triangleCount++;
                        triangleArea += t1.getMass();
                        totalArea += t1.getMass();
                        //Triangle created with velocity
                    }
                } else {
                    Triangle t1 = new Triangle(x, y, base, height);
                    t1.setColor(Color.BLACK);
                    box.addShape(t1);
                    triangleCount++;
                    triangleArea += t1.getMass();
                    totalArea += t1.getMass();
                    //Triangle created without velocity
                }
            } else if (shapeType.equalsIgnoreCase("Rectangle")) {
                int x = line.nextInt();
                int y = line.nextInt();
                int base = line.nextInt();
                int height = line.nextInt();
                if (line.hasNextInt()) {
                    int vx = line.nextInt();
                    int vy = line.nextInt();
                    if (line.hasNextInt()) {
                        int red = line.nextInt();
                        int green = line.nextInt();
                        int blue = line.nextInt();
                        Rectangle r1 = new Rectangle(x, y, base, height);
                        r1.setColor(new Color(red, green, blue));
                        r1.setVelocity(vx, vy);
                        box.addShape(r1);
                        rectangleCount++;
                        rectangleArea += r1.getMass();
                        totalArea += r1.getMass();
                        //Rectangle created with velocity and color
                    } else {
                        Rectangle r1 = new Rectangle(x, y, base, height);
                        r1.setVelocity(vx, vy);
                        r1.setColor(Color.BLACK);
                        box.addShape(r1);
                        rectangleCount++;
                        rectangleArea += r1.getMass();
                        totalArea += r1.getMass();
                        //Rectangle created with velocity
                    }
                } else {
                    Rectangle r1 = new Rectangle(x, y, base, height);
                    r1.setColor(Color.BLACK);
                    box.addShape(r1);
                    rectangleCount++;
                    rectangleArea += r1.getMass();
                    totalArea += r1.getMass();
                    //Rectangle created without velocity
                }
            } else {
                System.err.println(shapeType + " is not a recognized shape");
            }
        }
        System.out.println("Circle: " + circleCount + "      " + "Square: " + squareCount);
        System.out.println("Triangle: " + triangleCount + "    " + "Rectangle: " + rectangleCount);
        // System.out.println("Total Area is: " + (circleArea + squareArea + triangleArea + rectangleArea));
        // The operation above will lead to problems of calculation accuracy
        System.out.println("Total Area is: " + totalArea);
        PrintWriter writer = new PrintWriter("src/shapesArea_Anka.txt");
        // writer.println("Total Area is: " + (circleArea + squareArea + triangleArea + rectangleArea));
        // The operation above will lead to problems of calculation accuracy
        writer.println("Total Area is: " + totalArea);
        writer.close();
        box.start();
    }
}
